home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / ctlr.c < prev    next >
Text File  |  1987-06-16  |  23KB  |  872 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      ctlr.C
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.    ctlr
  21.    display main menu screen for word search
  22.    process request
  23. */
  24. #include "stdio.h"
  25. #include "dos.h"
  26. #include "dir.h"
  27. /*
  28.     xscr.h
  29.     header file for scan function of XDIR
  30.  
  31. */
  32. /*  xscan functions */
  33.  
  34. int xscan(char *fnam, char string[10][80],int ans[],char *options);
  35. int sbuff(char *buff,char *string, int max, char *options);
  36. int strxcmp(char *buff1, char *buff2, int maxlen, char *options);
  37. int strxchr(char *buff, char c, int max);
  38.  
  39. /* xword functions */
  40. void xword(char *buffer,char words[10][80]);
  41.  
  42. /* xscr functions */
  43. int ctlr(char *fnam);
  44. int xscr(char *fnam, char *xnam, char *stext,
  45.          int *sflag, int *stype, char *ext, char *opts);
  46. int getitem(int *item, int row, int col, char *text, int len);
  47. void sepname(char *all, char *path, char *file);
  48. void cline(int row, int col, int len);
  49.  
  50. /* sdir functions */
  51.  
  52. int sdir(char *buff, char *filename, int action, char ext[10][80],int type);
  53. int cmpext(char *fnam, char *ext);
  54.  
  55. /*********** end of header definition *************************/
  56.  
  57. /*
  58. main()
  59. {
  60.       ctlr("\\bin\\xdir.exe");
  61.  
  62. }
  63. */
  64. /*
  65.    ctlr
  66.    controller module
  67.  
  68. */
  69. ctlr(fnam)
  70. char *fnam;
  71. {
  72.      int  i,j, k, sflag, stype;
  73.      char xnam[80], stext[80], extents[80];
  74.      int  ans[10];               /* answer for search */
  75.      char words[10][80];         /* words to search */
  76.      char filename[20];          /* work area for file name */
  77.      char extnames[10][80];      /* extent names */
  78.      char options[80];           /* search options */
  79.      struct {
  80.        char snam[40];            /* search file name */
  81.        int  sopts[10];           /* search hits */
  82.        } sfiles[100];            /* store max of 100 files */
  83.      int scount;
  84.  
  85.      i = xscr(fnam,xnam,stext,&sflag,&stype,extents,options);
  86.  
  87.  
  88.      if (i == -1)
  89.         {writestr("\nSearch aborted ");
  90.         return(-1);
  91.         }
  92.  
  93.      cursor(2,50);
  94.      writestr("Begin search ... ");
  95.      cursor(5,10);
  96.      writestr("SEARCHING ");
  97.      cursor(7,10);
  98.      writestr("Located ");
  99.      scount = 0;
  100.  
  101.      for (i=0;i<10;i++) words[i][0] = 0;   /* init */
  102.      xword(stext,words);          /* xlate search text to words */
  103.  
  104.      if (stype == 0)       /* search a single file */
  105.         {
  106.         i = xscan(xnam,words,ans,options);
  107.         if (i != 1)
  108.             {
  109.             writestr("\nXscan failure ");
  110.             getch();
  111.             exit(0);          /* scan failure */
  112.             }
  113.         for (i=j=0;i<10;i++)
  114.            if (ans[i] == 1) ++j;      /* determine any hits */
  115.  
  116.         if (j)
  117.            {
  118.            writestr("\nSearch words found in file ");
  119.            writestr(xnam);
  120.            for (i=0;i<10;i++)
  121.              if (ans[i]==1) {putchar('\n');writestr(words[i]);}
  122.            }
  123.         else
  124.            {
  125.            writestr("\nNo match found in file ");
  126.            writestr(xnam);
  127.            }
  128.          }    /* end if stype  = 0 */
  129.  
  130.        else                    /* search a directory */
  131.         {
  132.            for (i=0;i<10;i++) extnames[i][0] = 0;   /* init */
  133.            xword(extents,extnames);          /* xlate search extents to words */
  134.  
  135.           i = sdir(xnam,filename,1,extnames,sflag);   /* open the directory */
  136.           while (i = sdir(xnam,filename,0,extnames,sflag))
  137.            {
  138.            cursor(5,23);
  139.            for (i=0;i<20;i++) putchar(' ');
  140.            cursor(5,23);
  141.            writestr(filename);
  142.            j = xscan(filename,words,ans,options);   /* search the file */
  143.            if (j != 1)
  144.               {
  145.               writestr("\nXscan failure");
  146.               getch();
  147.               exit(0);
  148.               }
  149.  
  150.            for (i=j=0;i<10;i++) if (ans[i] == 1) ++j;      /* determine any hits */
  151.  
  152.            if (j)
  153.              {
  154.              cursor(7,23);
  155.              for (i=0;i<20;i++) putchar(' ');
  156.              cursor(7,23);
  157.              if (scount < 100)
  158.                 {
  159.                 strcpy(sfiles[scount].snam,filename);
  160.                 for (i=0;i<10;i++) sfiles[scount].sopts[i] = ans[i];
  161.                 ++scount;
  162.                 }
  163.              for (i=0;i<10;i++)
  164.                if (ans[i]==1) {putchar(' ');writestr(words[i]);}
  165.              }
  166.           }        /* end while */
  167.          }    /* end if stype  != 0 */
  168.  
  169.         /*************** print out the results of search *************/
  170.         clrscr();
  171.         writestr("\nResults of search located text as follows: ");
  172.         for (i=k=0;((i<10)&&(words[i][0] != 0));i++) ++k;
  173.  
  174.         for (i=0;i<scount;i++)
  175.         {
  176.         writestr("\nFile ");
  177.         writestr(sfiles[i].snam);
  178.         for (j=0;j<k;j++)
  179.            if (sfiles[i].sopts[j] == 1) {putchar(' ');writestr(words[j]);}
  180.         }  /* end of dump loop */
  181.         writestr("\nPress any key to continue ");   /* pause screen */
  182.         getch();
  183.  
  184. }
  185.  
  186. /*
  187.    xscan
  188.    scan a file for text strings
  189.    to be included with xdir upon completion of module
  190.  
  191. */
  192. #define BUFFSIZE 8000
  193.  
  194. int  xscan(fnam,string,ans,options)
  195. char *fnam;
  196. char string[10][80]; /* search strings */
  197. int  ans[];          /* search results */
  198. char *options;       /* search options */
  199. {
  200.      int  fd, flen,i,j,k,l,m;
  201.      char *alloc(int amt);
  202.      char *buff;
  203.      /* char temp[20]; */
  204.  
  205.      fd = open(fnam,0);
  206.      if (fd == EOF)
  207.         {
  208.         writestr("\nCan't open file ");
  209.         writestr(fnam);
  210.         return(-1);
  211.         }
  212.  
  213.      buff = alloc(BUFFSIZE+1);
  214.      if (buff == NULL) return (-1);
  215.  
  216.      for (i=0;string[i][0] != 0;i++) ans[i] = 0;  /* init to false */
  217.      k = i;           /* count of search strings */
  218.  
  219.      flen = BUFFSIZE;
  220.  
  221.      while (flen == BUFFSIZE)
  222.      {
  223.        for (j=l=0;l<k;l++) j += ans[l];   /* determine if search s/b ended
  224.                                           because all strings located */
  225.        if (j == k) break;                 /* yes, all found */
  226.        flen = read(fd,buff,BUFFSIZE);
  227.  
  228.        for (i=0;string[i][0] != 0;i++)
  229.        {
  230.        if (ans[i] == 0)    /* only test where no match yet */
  231.           {
  232.           m = xcomp(buff,string[i],options,flen);   /* search buffer */
  233.           if (m == 0) ans[i] = 1;     /* record answer */
  234.           }
  235.        }
  236.      }
  237.  
  238.      free(buff);           /* free the buffer */
  239.      close(fd);            /* close the file */
  240.      return (1);
  241. }
  242.  
  243.  
  244. /*
  245.     xword
  246.     parse a string into words
  247.  
  248. */
  249.  
  250. void xword(buffer,words)
  251. char *buffer;   /* buffer to parse */
  252. char words[10][80];
  253. {
  254.  
  255.       int  i, j, state;
  256.       char *cp, *cpo;
  257.       int  c;
  258.       int  num;            /* used in octal numbers */
  259.       char temp[80];
  260.       int  tflag;          /* flag - current words in tic marks */
  261.  
  262.       for (i=0;i<10;i++) words[i][0] = 0;
  263.  
  264.       state = i = j = tflag = 0;
  265.       cp = buffer;
  266.       cpo = temp;
  267.  
  268.       while (1)
  269.       {
  270.       c = (int) *cp++;      /* get a character */
  271.  
  272.  
  273.           switch (state)
  274.           {
  275.           case 0:           /* begin a new word */
  276.             tflag = 0;
  277.             cpo = temp;
  278.             if (c == 0) state = 98;
  279.             else if (c == ' ') state = 0;     /* skip leading spaces */
  280.             else if (c == '\'') {tflag = 1;state = 1;}
  281.             else {*cpo++ = c;state = 1;}
  282.             break;
  283.  
  284.           case 1:           /* start a new word tick mark */
  285.             if (c == '\\') state = 2;
  286.             else if ((c == '\'') && tflag)
  287.                 {*cpo = 0;state = 0;strcpy(words[i++],temp);}
  288.             else if ((c == ' ') && (tflag == 0))
  289.                 {*cpo = 0;state = 0;strcpy(words[i++],temp);}
  290.  
  291.             else if (c == 0) state=98;
  292.  
  293.             else {*cpo++ = c;stat